<?php
	if(!isset($_SESSION['member_is_admin']) || $_SESSION['member_is_admin'] == ''){
		header("Location: index.php");
	}
	//--
	$xtpl_main->assign ( "page_title", "Category Task" );
	//--
	$cateStatusIsChecked = true;
	$frmActValue = 'doCategorySave';
	
	if(isset($_GET['cate_id'])){
		$resultFetchCategoryInformation = mysql_query("SELECT * FROM category WHERE cate_id = '".$_GET['cate_id']."'");
		if(mysql_num_rows($resultFetchCategoryInformation) < 1){
			$xtpl->parse('center.category_task_error');
		}
		else {
			$rowFetchCategoryInformation = mysql_fetch_array($resultFetchCategoryInformation);
			if($rowFetchCategoryInformation['cate_status'] == '0'){
				$cateStatusIsChecked = FALSE;
			}
			$frmActValue = 'doCategoryUpdate';
			$xtpl->assign('cate_name', $rowFetchCategoryInformation['cate_name']);
			$xtpl->assign('editing_cate_id', $_GET['cate_id']);
		}
	}
	//--
	if($cateStatusIsChecked == true){
		$xtpl->assign('cateStatusIsChecked', 'checked="checked"');
	}
	$xtpl->assign('frmActValue', $frmActValue);
	
	//-- Save
	if(isset($_POST['frmAct'])){
		if($_POST['frmAct'] == 'doCategorySave'){
			if(checkCateName($_POST['cate_name'], '') == true){
				mysql_query("INSERT INTO category (cate_name, cate_status) VALUES ('".addslashes($_POST['cate_name'])."', '".$_POST['cate_status']."')");
				header("Location: ?mod=admin&act=category");
			}else{
				$xtpl->parse('center.category_name_existed');
			}
		}
		if($_POST['frmAct'] == 'doCategoryUpdate'){
			if(checkCateName($_POST['cate_name'], $_POST['cate_id']) == true){
				mysql_query("UPDATE category SET cate_name = '".addslashes($_POST['cate_name'])."', cate_status = '".$_POST['cate_status']."' WHERE cate_id = '".$_POST['cate_id']."'");
				header("Location: ?mod=admin&act=category");
			}else{
				$xtpl->parse('center.category_name_existed');
			}
		}
	}
	
	function checkCateName($cateName, $cate_id){
		if($cate_id != ''){
			$rs = mysql_query("SELECT cate_name FROM category WHERE cate_name = '".addslashes($cateName)."' AND cate_id <> '".addslashes($cate_id)."'");
		}else{
			$rs = mysql_query("SELECT cate_name FROM category WHERE cate_name = '".addslashes($cateName)."'");
		}
		
		if(mysql_num_rows($rs) > 0){
			return false;
		}else{
			return true;
		}
	}
?>